﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SER = CPPEI.ETL.Domain.Services;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using CPPEI.ETL.Domain;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 获取第三方数据表
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("TPT", "GetList", "1.0")]
    public class GetListAction : BaseAction<GetListParams, GetListResult>
    {
        protected override ActionResult<GetListResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetListParams opParams)
        {
            GetListResult result = new GetListResult();

            PagedList<Domain.Data.ThirdPartyTable> serResult = new SER.DtTableService().GetThirdPartyTables(opParams.Filter, opParams.Page * 15, 15);
            if (serResult.IsSuccess)
            {
                result.Start = serResult.Start;
                result.Limit = serResult.Limit;
                result.Page = opParams.Page;
                result.Total = serResult.Total;

                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetListResult.Item item = new GetListResult.Item();
                    item.Id = serResult.Data[index].Id;
                    item.Name = serResult.Data[index].Name;

                    result.Items.Add(item);
                }
                return new ActionResult<GetListResult>(result);
            }
            else
            {
                return new ActionResult<GetListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetListParams
    {
        public string Filter { get; set; }
        public int Page { get; set; }
    }

    public class GetListResult : PagedResult
    {
        public List<Item> Items { get; set; }

        public GetListResult()
        {
            Items = new List<Item>();
        }

        public class Item
        {
            /// <summary>
            /// 表id
            /// </summary>
            public int Id { get; set; }

            /// <summary>
            /// 表名称
            /// </summary>
            public string Name { get; set; }
        }
    }
}
